<?php

namespace Console\Controller;

use Common\Controller\AdminAuthController;
use Console\Common\Library\Page;

class AdController extends CstBaseController
{
    private $adLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->adLogic = A('Console/Ad', 'Logic');
    }

    /**
     * 获取我发布的任务列表
     */
    public function doGetList()
    {
        $data = I('post.');
        $page = $data['page'] ? $data['page'] : 1;
        $pageSize = 15;
        unset($data['page']);

        $listRes = $this->adLogic->getList($page, $pageSize, $data);
        $data = $listRes['Result'];
        $total = $data['total'];
        $list = $data['list'];

        $PageLib = new Page($total, $pageSize);
        $pageHtml = $PageLib->show();
        $pageTotal = ceil($total / $pageSize);

        $typeDic = $this->adLogic->getTypeDic();

        $this->assign("typeDic", $typeDic);
        $this->assign("total", $total);
        $this->assign("pageTotal", $pageTotal);
        $this->assign("pageHtml", $pageHtml);
        $this->assign("list", $list);
        $this->assign("pageHtml", $pageHtml);
        $html = $this->fetch('doGetAdList');

        $res = array(
            "list" => $list,
            "html" => $html,
            "total" => $total,
            "pageTotal" => $pageTotal
        );
        $this->ajaxReturn(static::$res->suc("成功", $res));
    }

    /**
     * 广告列表
     *
     * @return void
     */
    public function adList()
    {
        $typeDic = $this->adLogic->getTypeDic();
        $this->assign("typeDic", $typeDic);
        $this->display();
    }

    /**
     * 添加页面
     *
     * @return void
     */
    public function addAd()
    {
        $typeDic = $this->adLogic->getTypeDic();
        $this->assign("typeDic", $typeDic);
        $this->display();
    }


    public function add()
    {
        $this->display();
    }

    /**
     * 编辑页面
     *
     * @return void
     */
    public function edit()
    {
        $id = I('get.id', 0, intval);
        if (empty($id)) {
            $this->sysErr("广告ID不能为空");
        }
                
        $where = array(
            "id"=>$id
        );
        $info = $this->adLogic->find($where);

        if (empty($info)) {
            $this->sysErr("未找到该广告");
        }

        $this->assign('id', $id);
        $this->assign('info', $info);
        $this->display();
    }

    /**
     * 添加动作
     *
     * @return void
     */
    public function doAdd()
    {
        $data = I('post.');
        $imageUrl=explode(',', rtrim($data['cover'], ','));
        $data['cover']=$imageUrl[0];

        if (empty($data["title"])) {
            $this->ajaxReturn(static::$res->err("标题不能为空"));
        }

        if (!empty($data["url"])) {
            if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $data["url"])) {
                $this->ajaxReturn(static::$res->err("链接地址格式异常"));
            }
        }

        $userInfo = $this->getUserInfo();
        $data["add_user"]= $userInfo['id'];
        $data["add_time"]= date('Y-m-d H:i:s');

        $res = $this->adLogic->add($data);
        $this->ajaxReturn($res);
    }
    /**
     * 编辑动作
     *
     * @return void
     */
    public function doEdit()
    {
        $data = I('post.');
        $id = $data["id"];
        unset($data["id"]);
        if (empty($id)) {
            $this->ajaxReturn(static::$res->err("广告ID不能为空"));
        }

        $imageUrl=explode(',', rtrim($data['cover'], ','));
        $data['cover']=$imageUrl[0];
        
        if (empty($data["title"])) {
            $this->ajaxReturn(static::$res->err("标题不能为空"));
        }
        
        if (!empty($data["url"])) {
            if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $data["url"])) {
                $this->ajaxReturn(static::$res->err("链接地址格式异常"));
            }
        }

        $userInfo = $this->getUserInfo();
        $data["update_user"]= $userInfo['id'];
        $data["update_time"]= date('Y-m-d H:i:s');

        $where = array(
            "id"=>$id
        );

        $res = $this->adLogic->save($where, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 删除动作
     *
     * @return void
     */
    public function doDel()
    {
        $id = I('post.id');
        $where = array(
            "id"=>$id
        );
        
        $data = array(
            "state"=>-1
        );

        $res = $this->adLogic->save($where, $data);
        $this->ajaxReturn($res);
    }

    /**
     * 开启动作
     *
     * @return void
     */
    public function doOpen()
    {
        $id = I('post.id');
        $where = array(
            "id"=>$id
        );
        
        $data = array(
            "state"=>1
        );
        
        $res = $this->adLogic->save($where, $data);
        $this->ajaxReturn($res);
    }
}
